﻿using Blm.Utils.Extensions;
using Language.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Ms;
using static VisionCloud.Core.Model.Sys.SysUser;

namespace VisionCloud.Api.Controllers.Ms
{
    /// <summary>
    /// 报表数据模块
    /// </summary>
    [SystemModule("报表数据")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/ms/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "报表数据")]
    public class ReportController : ApiBaseController
    {
        /// <summary>
        /// 多语言资源
        /// </summary>
        public IStringLocalizer<ServiceResource> Localizer { get; set; }

        public IStringLocalizer<CommonResource> CommonLocalizer { get; set; }

        public MsReportService msReportService { get; set; }

        public ApartmentReportService ApartmentService { get; set; }

        /// <summary>
        /// 获取项目报告列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        [SwaggerResponse(0, "获取报告列表的字段：Result", typeof(ReportDto))]
        [HttpPost, SwaggerOperation(Tags = new[] { "数据报告" })]
        public async Task<ApiResult> GetList(ReportListSearchDto condition)
        {
            condition.UserId = TokenModel.YunUserGuid;
            condition.UserType = (EnumUserType)TokenModel.UserType;
            return await TryCatchAsync(msReportService.GetReportList(condition));
        }

        /// <summary>
        /// 获取任务的gltf文件列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "数据报告" })]
        [SwaggerResponse(0, "获取gltf文件列表的字段：Result", typeof(ModelingGltfDto))]
        public async Task<ApiResult> GetGltfList(TaskIdWithHouseTypeDto dto)
        {
            return await msReportService.GetGltfListAsync(dto);
        }

        /// <summary>
        /// 获取任务的分户报告
        /// </summary>
        /// <remarks>
        /// <code>
        /// {  
        ///     "Code": 0,  
        ///     "Result": [""] //url数组  
        /// }
        /// </code>
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "数据报告" })]
        public async Task<ApiResult> GetApartmentReports([FromBody] TaskIdsDto dto)
        {
            await Contracts.Frequency($"user_downapartmentreport_limit_{TokenModel.Id}", 1, TimeSpan.FromSeconds(3), CommonLocalizer["YB0449"].Value);

            if (dto?.TaskIds.IsEmpty() ?? true)
                return new ApiResult { Code = AjaxCode.Fail, Message = "参数TaskIds格式错误" };

            if (dto.TaskIds.Count > 30)
                return new ApiResult { Code = AjaxCode.Fail, Message = string.Format(Localizer["LimitDownloadReport"].Value, 30) };

            var list = await ApartmentService.GetReportUrl(dto.TaskIds);
            if (list.IsEmpty())
                return new ApiResult { Code = AjaxCode.Fail, Message = "无分户报告" };

            return new ApiResult { Code = AjaxCode.Success, Result = list };
        }

        /// <summary>
        /// 外立面数据报告下载
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "数据报告" })]
        public async Task<ApiResult> ExternalDownload([FromBody] TaskIdsDto dto)
        {
            await Contracts.Frequency($"user_downreport_limit_{TokenModel.Id}", 1, TimeSpan.FromSeconds(3), CommonLocalizer["YB0449"].Value);
            return await TryCatchAsync(msReportService.DownloadExternalReport(dto.TaskIds));
        }
    }
}